//
//  TDVisitorView.swift
//  微博
//
//  Created by tong dan on 16/3/1.
//  Copyright © 2016年 tong dan. All rights reserved.
//

import UIKit
import SnapKit

class TDVisitorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 提供一个方法供外界去设置当前访客视图里面的内容
    
    /// 设置当前访客视图的内容
    ///
    /// - parameter imgNamed: <#imgNamed description#>
    /// - parameter message:  <#message description#>
    
    func setInfo(imgNamed: String?,message: String?){
        
        if let name = imgNamed{
            
            iconView.image = UIImage(named: name)
            
        }else{
            // 进入到这个地方,代表是首页,因为首页传入的 imgNamed 为 nil
            animation()
            
        }
        
        messageLabel.text = message
        
    }
    
    
    
    
    //圆圈执行动画
    private func animation(){
        
        //要执行动画就让它显示出来
        circleView.hidden = false
        //初始动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //结束值
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //执行一次动画所需要的时间
        anim.duration = 20
        //为避免切换页面或者退出到桌面后再进入动画不转，也就是动画被释放到情况
        anim.removedOnCompletion = false
        //让圆圈转起来
        circleView.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    
    //懒加载控件
    // 大图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var circleView: UIImageView = {let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        //当不执行动画时让圆圈的view隐藏，不让它显示在其他页面
        imageView.hidden = true
        return imageView
    }()
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        //设置换行
        label.numberOfLines = 0
        //文字居中
        label.textAlignment = .Center
        return label
    }()
    
    //注册
    lazy var registerButton: UIButton = {
        let button = UIButton()
        
        
        //设置文字以及大小
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setTitle("注册", forState:UIControlState.Normal)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    
    
    //登录
    lazy var loginButton:UIButton = {
        
        let button = UIButton()
        //设置文字以及大小
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setTitle("登录", forState:UIControlState.Normal)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    
    //阴影
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage (named: "visitordiscover_feed_mask_smallicon"))
    
}
    //设置视图
    extension TDVisitorView{
        
        private func setupUI(){
            
            backgroundColor = UIColor(white: 237/255.0, alpha: 1)
            
            //添加控件
            addSubview(circleView)
            addSubview(maskIconView)
            addSubview(iconView)
            addSubview(messageLabel)
            addSubview(registerButton)
            addSubview(loginButton)
            
           //添加约束
           iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            }
            circleView.snp_makeConstraints { (make) -> Void in
                make.center.equalTo(iconView)
            }
            messageLabel.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(circleView.snp_bottom).offset(16)
                make.centerX.equalTo(circleView)
                make.width.equalTo(244)
            }
            registerButton.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(messageLabel)
                make.top.equalTo(messageLabel.snp_bottom).offset(16)
                make.width.equalTo(100)
            }
            loginButton.snp_makeConstraints { (make) -> Void in
                make.trailing.equalTo(messageLabel)
                make.top.equalTo(registerButton)
                make.width.equalTo(100)
            }
            maskIconView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(self)
                make.leading.equalTo(self)
                make.trailing.equalTo(self)
                make.bottom.equalTo(registerButton)
            }
            
        }
        
        
        //设置视图系统添加约束
        //设置视图
        //    private func test(){
        //
        //        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //
        //        //添加控件
        //        addSubview(circleView)
        //        addSubview(maskIconView)
        //        addSubview(iconView)
        //        addSubview(messageLabel)
        //        addSubview(registerButton)
        //        addSubview(loginButton)
        //        //添加约束
        //        //大图标
        //        // 使用 autolayout 关闭 autoresizing
        //        iconView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        //
        //        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        //
        //
        //        //圆圈
        //        circleView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        //
        //        //消息
        //        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        //
        //
        //        //注册
        //        registerButton.translatesAutoresizingMaskIntoConstraints = false
        //
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //        // 约束宽度
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //
        //
        //        // 登录
        //        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute:NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //
        //
        //        // 阴影
        //        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[View]-0-|", options: [], metrics: nil, views: ["View": maskIconView]))
        //        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[View]-(margin)-[registerbutton]", options: [], metrics: ["margin":-35], views: ["View":maskIconView,"registerbutton":registerButton]))
        //        
        //    }
}




